﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;
using 后整理管理系统.自定义类;
using 纺织贸易管理系统.其他窗体;
using 纺织贸易管理系统.设置窗体;
using 纺织贸易管理系统.选择窗体;

namespace 纺织贸易管理系统.新增窗体
{
    public partial class 委外取货单 : Form
    {
        public FormUseful useful { get; set; } = FormUseful.新增;
        public List<danjumingxitable> danjumingxitables = new List<danjumingxitable>();       
        public DanjuTable danju = new DanjuTable();
        public 委外取货单()
        {
            InitializeComponent();           
            CreateGrid.Create(this.Name , gridView2);
            //gridView1.Columns["Remarkers"].ColumnEdit = cmddanwei;
            if (gridView2.Columns["Bianhao"] != null)
            {
                gridView2.Columns["Bianhao"].ColumnEdit = ChengpingButtonEdit1;
            }
            if (gridView2.Columns["pingming"] != null)
            {
                gridView2.Columns["pingming"].ColumnEdit = ChengpingButtonEdit1;
            }
            if (gridView2.Columns["OrderNum"] != null)
            {
                gridView2.Columns["OrderNum"].ColumnEdit = ChengpingButtonEdit1;
            }
            if (gridView2.Columns["ganghao"] != null)
            {
                gridView2.Columns["ganghao"].ColumnEdit = ChengpingButtonEdit1;
            }
            gridControl2.DataSource = danjumingxitables;
            cmbgongyi.DataSource = 委外取货单BLL.获取加工类型();
        }
        private void txtkehu_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
           var fm = new 供货商选择() { linkman=new LXR() { ZJC="",MC="" } };
            fm.ShowDialog();
            danju.ksbh = fm.linkman.BH;
            danju.ksmc = fm.linkman.MC;
            txtkehu.Text = danju.ksmc;
        }

        private void txtwuliu_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var fm = new 物流选择();
            fm.ShowDialog();
            danju.wuliuBianhao  = fm.linkman.Bianhao  ;
            txtwuliu.Text = fm.linkman.name ;
            var wuliu = fm.linkman;
            txtchepai.Text = wuliu.CarNum;
            txtlianxiren.Text = wuliu.name;
            txtQicheleixing.Text = wuliu.CarLeixing;
            
        }

        private void txtckmc_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if(cmbcunfang.Text=="仓库")
            {
                var fm = new 仓库选择() { stock = new StockInfoTable() { mingcheng = cmbcunfang.Text } };
                fm.ShowDialog();
                txtckmc.Text = fm.stock.mingcheng;
                CreateGrid.CreateKuweiCmb(gridView2, txtckmc.Text);
            }
            else
            {
                var fm = new 供货商选择() { linkman = new LXR() { MC ="",ZJC="" } };
                fm.ShowDialog();
                txtckmc.Text = fm.linkman.MC;
                CreateGrid.ClearKuwei(gridView2);
            }
        }
 
        private void 添加行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            danjumingxitables.Add(new danjumingxitable() { danhao = txtdanhao.Text, rq = Convert.ToDateTime(dateEdit1.Text) });
        }
        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridView2.CloseEditor();
            if (txtckmc.Text=="")
            {
                MessageBox.Show("收货地址没有选择！请填写地址", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (danjumingxitables.Where(x => x.pingming  != null).ToList().Count == 0)
            {
                ShowMessageBox.ShowErrorBox (  "没有检测到任何布料信息.请选择相应的布料后再保存！");
                return;
            }
            InitDanju();
            try
            {
                Connect.dbcontext.BeginTran();
                if (useful == FormUseful.新增)
                {
                    委外取货单BLL.保存单据(danju, danjumingxitables);
                }
                else
                {
                    委外取货单BLL.修改单据(danju, danjumingxitables);
                }
                Connect.dbcontext.CommitTran();
                AlterDlg.Show("保存成功！");
                if (ShowMessageBox.ShowAskBox("是否继续新增操作？") == DialogResult.Yes)
                {
                    //清空所有控件
                    Init();
                }
                else
                {
                    MainForm.mainform.CloseTab();
                }
            }
            catch(Exception ex)
            {
                Connect.dbcontext.RollbackTran();
                ShowMessageBox.ShowErrorBox(ex.Message);
            }
        }
        private void Init()
        { 
            foreach (Control  c in this.groupControl1.Controls  )
            {
                if(c is DevComponents.DotNetBar.Controls.TextBoxX)
                {
                    c.Text = "";
                }
                if(c is DevExpress.XtraEditors.ButtonEdit)
                {
                    c.Text = "";
                }
            }
            dateEdit1.DateTime = DateTime.Now.Date;
            txtdanhao.Text = BianhaoBLL.CreatDanhao(FirstLetter.委外取货单 , dateEdit1.DateTime, DanjuLeiXing.委外取货单);
            danju.dh = txtdanhao.Text;
            danjumingxitables = danjumingxitableService.Getdanjumingxitablelst(x => x.danhao == txtdanhao.Text);
            var length = danjumingxitables.Count;
            for (int i = 0; i < 30 - length; i++)
            {
                danjumingxitables.Add(new danjumingxitable() { danhao = txtdanhao.Text,rq=dateEdit1.DateTime  });
            }
            if (cmbcunfang.Text == "仓库")
            {
                txtckmc.Text = QueryTime.DefaultStock;
            }
            gridControl2.DataSource = danjumingxitables;
            useful = FormUseful.新增;
        }

        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            var fm = new 配置列(gridView2) { formname = this.Name, Obj = new danjumingxitable() };
            fm.ShowDialog();
        }

        private void 委外取货单_Load(object sender, EventArgs e)
        {
            if (useful == FormUseful.新增)
            {
               
                Init();
            }
            else
            {
                Edit();
                if(useful==FormUseful.查看 )
                {
                    保存ToolStripMenuItem.Enabled = false;
                }
            }
            
        }
        private void Edit()
        {
            txtdanhao.Text = danju.dh;
            txtbeizhu.Text = danju.bz;
            txtchepai.Text = danju.CarNum;        
            cmbcunfang.Text = danju.StockName ;
            txtkehu.Text = danju.ksmc;
            txtlianxidianhua.Text = danju.lianxidianhua;
            txtlianxiren.Text = danju.lianxiren;
            txtQicheleixing.Text = danju.CarLeixing;
            txtwuliu.Text = danju.wuliugongsi;
            txtyunfei.Text = danju.yunfei.ToString();
            cmbqiankuan.Text = danju.Qiankuan;
            comhanshui.Text = danju.Hanshui;
            cmbgongyi.Text = danju.jiagongleixing;
            txtyaoqiu.Text = danju.yaoqiu;
            dateEdit1.Text = danju.rq.ToShortDateString();
            txtckmc.Text = danju.ckmc;
            danjumingxitables = danjumingxitableService.Getdanjumingxitablelst(x => x.danhao == txtdanhao.Text);
            var length = danjumingxitables.Count;
            for (int i = 0; i < 30 - length; i++)
            {
                danjumingxitables.Add(new danjumingxitable() { danhao = txtdanhao.Text, rq = dateEdit1.DateTime });
            }
            gridControl2.DataSource = danjumingxitables;
        }

        private void ChengpingButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (txtckmc.Text == "")
            {
                MessageBox.Show("请先选择仓库名称", "错误", MessageBoxButtons.OK);
                return;
            }
            var fm = new 库存选择() { StockName = txtkehu .Text };
            fm.ShowDialog();
            SelectHelper.StockConverToDanjumingxi(gridView2, fm.pingzhong, danjumingxitables, txtdanhao.Text, dateEdit1.DateTime, DanjuLeiXing.委外加工单);
            gridControl2.RefreshDataSource();
            gridView2.CloseEditor();
        }
        private void txtkehu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                var fm = new 供货商选择() { linkman = new LXR() { ZJC = txtkehu.Text, MC = "" } };
                fm.ShowDialog();
                danju.ksbh = fm.linkman.BH;
                danju.ksmc = fm.linkman.MC;
                txtkehu.Text = danju.ksmc;
            }
        }
        private void 保存样式ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            CreateGrid.SaveGridview(this.Name, gridView2);
        }

        private void cmbcunfang_TextChanged(object sender, EventArgs e)
        {
            if (useful == FormUseful.新增)
            {
                if (cmbcunfang.Text == "仓库")
                {
                    txtckmc.Text = QueryTime.DefaultStock;
                }
            }
        }

        private void gridView2_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                danjumingxitables[gridView2.FocusedRowHandle].hanshuiheji = danjumingxitables[gridView2.FocusedRowHandle].hanshuidanjia * danjumingxitables[gridView2.FocusedRowHandle].chengpingmishu;
                gridControl2.RefreshDataSource();
            }
            catch
            {
                MessageBox.Show("请输入数字。计算错误");
            }
        }

        private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintAzhijia(PrintModel.Privew);
        }
        private void PrintAzhijia(PrintModel useful)
        {
            InitDanju();
            new Tools.打印发货单()
            {
                danjuTable = danju,
                danjumingxitables = danjumingxitables.Where(x => !string.IsNullOrWhiteSpace(x.pingming)).ToList(),
                danjuinfo = new Tools.FormInfo() { FormName = "销售发货单查询", GridviewName = gridView2.Name },
                mingxiinfo = new Tools.FormInfo() { FormName = this.Name, GridviewName = gridView2.Name }
            }.Print(PrintPath.报表模板 + "委外取货单.frx", useful);
            if (Sunny.UI.UIMessageBox.ShowAsk("该单据已打印！是否立即保存该单据"))
            {
                保存ToolStripMenuItem_Click(null, null);
            }
        }

        private void InitDanju()
        {
            danju.bz = txtbeizhu.Text;
            danju.CarLeixing = txtQicheleixing.Text;
            danju.CarNum = txtchepai.Text;
            danju.ckmc = txtckmc.Text;
            danju.StockName = cmbcunfang.Text;
            danju.dh = txtdanhao.Text;
            danju.djlx = DanjuLeiXing.委外取货单;
            danju.rq = dateEdit1.DateTime;
            danju.shouhuodizhi = txtckmc.Text;
            danju.lianxiren = txtlianxiren.Text;
            danju.Qiankuan = cmbqiankuan.Text;
            danju.Hanshui = comhanshui.Text;
            danju.je = danjumingxitables.Sum(x => x.hanshuiheji) + danjumingxitables.Sum(x => x.weishuiheji);
            danju.totaljuanshu = danjumingxitables.Sum(x => x.chengpingjuanshu);
            danju.TotalMishu = danjumingxitables.Sum(x => x.hanshuiheji);
            danju.wuliugongsi = txtwuliu.Text;
            danju.yunfei = (decimal)txtyunfei.Value;
            danju.lianxidianhua = txtlianxidianhua.Text;
            danju.jiagongleixing = cmbgongyi.Text;
            danju.yaoqiu = txtyaoqiu.Text;
            danju.own = Tools.CurrentUser.user.YHBH;
            ////检查库存。没有不能发货
            danjumingxitables.ForEach(x => x.suolv = 0);
            ///////
        }

        private void 打印编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintAzhijia(PrintModel.Design);
        }

        private void 直接打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintAzhijia(PrintModel.Print );
        }
    }
}
